<template>
  <div class="login-box">
    <div class="content">
      <!-- 左侧log -->
      <div class="contentLeft">
        <h1>物美智能 - 物联美好生活</h1>
        <h2>wumei smart open source living iot platform</h2>
        <div class="btnsBox">
          <span>登录方式</span>
          <div class="btn btnWX">微信</div>
          <div class="btn btnQQ">QQ</div>
          <div class="btn btnZFB">支付宝</div>
        </div>
        <div class="Bigbtns">
          <div class="btn">返回官网</div>
          <div class="btn">查看文档</div>
          <div class="btn" @click="goRegister">注册账号</div>
        </div>
        <div class="adminBox">
          <div>
            演 示
            <br />账 号
          </div>
          <ul>
            <li>
              受限管理
              <span>wumei</span>
              <span>123456</span>
            </li>
            <li>
              租户账号
              <span>wumei-ti</span>
              <span>123456</span>
            </li>
            <li>
              普通用户
              <span>wumei-u1</span>
              <span>123456</span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 右侧登陆框 -->
      <div class="contentRight">
        <h3 class="title">账号登录</h3>
        <!-- 表单 -->
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              type="text"
              v-model="ruleForm.username"
              autocomplete="off"
              prefix-icon="el-icon-s-custom"
              placeholder="账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
              prefix-icon="el-icon-s-goods"
              placeholder="密码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="checkPass" class="VerificationCodeImg">
            <el-col :span="13" style="padding-left: 0px; padding-right: 5%">
              <el-input
                type="text"
                v-model="ruleForm.loginCode"
                autocomplete="off"
                placeholder="验证码"
                class="VerificationCodeImg_inp"
              ></el-input>
            </el-col>
            <el-col :span="9">
              <img
                :src="userLoginImg"
                @click="getLoginImg"
                class="getLoginImg_class"
                alt
                height="40px"
              />
            </el-col>
          </el-form-item>
          <div class="remember">
            <el-checkbox v-model="remember">记住账号和密码</el-checkbox>
          </div>
          <el-form-item class="fromButton">
            <el-button
              type="primary"
              @click="submit('ruleForm')"
              :loading="loginLoading"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import { code, login } from '../api/index';
// import { v4 as uuidv4 } from 'uuid';
import _ from "@/assets/utils";

export default {
  name: "wm-login",
  loginCodeuuid: "",
  data() {
    // 校验规则
    const checkAccount = (_, value, callback) => {
      if (value.length === 0) {
        callback(new Error("账号不能为空!"));
        return;
      }
      let reg1 = /^[a-zA-Z0-9_-]{4,16}$/,
        reg2 = /^(?:(?:\+|00)86)?1\d{10}$/;
      if (!reg1.test(value) && reg2.test(value)) {
        callback(new Error("账号格式不正确!"));
        return;
      }
      callback();
    };
    //校验密码规则
    const checkPassword = (_, value, callback) => {
      if (value.length === 0) {
        callback(new Error("密码不能为空!"));
        return;
      }
      let reg = /^[a-zA-Z0-9_-]{6,16}$/;
      if (!reg.test(value)) {
        callback(new Error("密码格式不正确!"));
        return;
      }
      callback();
    };

    return {
      loginLoading: false,
      imgsrc: "",
      userLoginCode: "",
      ruleForm: {
        username: "",
        password: "",
        loginCode: "",
      },
      rules: {
        username: [{ validator: checkAccount, trigger: "blur" }],
        password: [{ validator: checkPassword, trigger: "blur" }],
      },
      remember: true,
    };
  },

  computed: {
    userLoginImg() {
      let userLoginImg = "data:image/gif;base64," + this.imgsrc;
      return userLoginImg;
    },
  },
  created() {
    this.getLoginImg();
    // 记住密码
    let remb = _.storage.get("remember");
    if (!remb) return;
    this.ruleForm.username = remb.username;
    this.ruleForm.password = remb.password;
  },
  mounted() {},

  methods: {
    async getLoginImg() {
      try {
        let res = await this.$http.getVerificationCode();
        if (res.code === 200) {
          this.imgsrc = res.img;
          this.loginCodeuuid = res.uuid;
        } else {
          this.$message.error("验证码获取失败");
        }
      } catch (_) {
        this.$message.error("验证码获取失败");
      }
      this.ruleForm.loginCode = "";
    },
    async submit() {
      // _.storage.remove("tk");
      this.loginLoading = true;
      try {
        let { loginCode, password, username } = this.ruleForm;
        let response = await this.$http.login({
          code: loginCode,
          password: this.ruleForm.password,
          username: this.ruleForm.username,
          uuid: this.loginCodeuuid,
        });
        // 处理成功情况
        this.userLoginCode = response.code;
        if (+this.userLoginCode == 200) {
          // 登录成功：存储Token、获取登陆者信息（存储到vuex中）、记住账号密码、提示&跳转
          localStorage.token = response.token;
          // _.storage.remove("tk");
          // 记住账号密码
          if (this.remember) {
            _.storage.set("remember", {
              username,
              password,
            });
          } else {
            _.storage.remove("remember");
          }
          // 1. 使用query获取路由中的问号传参
          let { to } = this.$route.query;
          // 2. 并且使用replace替换掉当前历史记录
          to ? this.$router.replace(to) : this.$router.push("/index");
          this.$message.success("登陆成功");
        } else {
          this.getLoginImg();
          let errortext = response.msg;
          this.$message.error(errortext);
        }
      } catch (_) {
        this.getLoginImg();
        this.$message.error("网络异常");
      }
      this.loginLoading = false;
    },
    // 切换注册
    goRegister() {
      this.$router.push("register");
    },
  },
};
</script>

<style lang="less" scoped>
.login-box {
  background: linear-gradient(303deg, #48c6ef 10%, #6f86d6 80%);
  height: 100%;
  overflow: auto;
  padding-top: 100px;
}
.content {
  display: flex;
  width: 1200px;
  margin: auto;
}
.contentLeft {
  width: 710px;
  color: #fff;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  h1,
  h2 {
    color: #fff;
  }
  h1 {
    font-size: 46px;
  }
  h2 {
    font-size: 26px;
  }
}
.contentRight {
  width: 370px;
  height: 365px;
  box-shadow: 0 20px 30px 0 rgb(0 0 0 / 10%);
  border-radius: 10px;
  background-color: hsla(0, 0%, 98%, 0.8);
  border: 1px solid #fff;
  padding: 25px 25px 5px 25px;
  margin: 0 auto;
  max-width: 370px;
  margin-bottom: 100px;
}
.btnsBox {
  display: flex;
  margin: 30px 0px 20px 0px;
  width: auto;
  span {
    margin-right: 10px;
    font-size: 16px;
  }
  line-height: 28px;
  .btn {
    width: 70px;
    height: 28px;
    border: 1px solid #fff;
    color: #fff;
    font-size: 14px;
    line-height: 28px;
    text-align: center;
    border-radius: 3px;
  }

  .btnWX {
    background-color: #89e7b3;
  }
  .btnQQ {
    background-color: #ffa4a4;
    margin: 0px 8px;
  }
  .btnZFB {
    background-color: #8cc8ff;
  }
}
.Bigbtns {
  display: flex;
  .btn {
    width: 95px;
    height: 40px;
    font-size: 14px;
    color: #fff;
    line-height: 40px;
    text-align: center;
    border-radius: 5px;
  }
  .btn:first-child {
    background: #ffba00;
  }
  .btn:nth-child(2) {
    background: #13ce66;
    margin: 0px 8px;
  }
  .btn:nth-child(3) {
    background: #fff;
    color: #000;
    cursor: pointer;
  }
}
.adminBox {
  display: flex;
  border: 1px solid rgb(255, 255, 255);
  margin-top: 30px;
  width: 300px;
  div {
    font-size: 18px;
    width: 75px;
    padding: 15px;
    line-height: 28px;
    font-weight: bold;
    box-sizing: border-box;
    background: rgb(245, 108, 108);
    margin: 2px;
  }
  ul {
    flex: 1;
  }
  li {
    font-size: 14px;
    height: 26px;
    line-height: 26px;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-between;
    padding: 0px 15px;
  }
}

.el-form-item button {
  width: 370px;
}

h3.title {
  font-size: 28px;
  text-align: center;
  margin-bottom: 20px;
  font-weight: 500;
}
.remember {
  margin-bottom: 20px;
}
</style>